
#include <tenacitas_text_log_file/logger.h> 

using namespace tenacitas::text_log_file;



// #############################################################################
// change_file_policy

// -----------------------------------------------------------------------------
std::string change_file_policy::m_dir_log( "./" );
std::string change_file_policy::m_pgm_name ( "NO-PGM" );
int64_t change_file_policy::m_pid ( -1 );
uint64_t change_file_policy::m_max_file_size( 1024 * 1014 * 5 );


// -----------------------------------------------------------------------------
change_file_policy::ptr
change_file_policy::
create( boost::thread::id p_thread_id ) {
    if ( m_pid == -1 ) {
        throw make_tenacitas_exception ("Process ID not configured for "
                                        "change_file_policy ");
    }
    return ptr( new change_file_policy( p_thread_id ) );
}


// -----------------------------------------------------------------------------
change_file_policy::
change_file_policy( boost::thread::id p_thread_id )
    : m_thread_id( p_thread_id ),
      m_file_index( -1 ),
      m_current_file_name( ){}


// -----------------------------------------------------------------------------
void
change_file_policy::
config( const std::string & p_dir_log,
        const std::string & p_pgm_name,
        int64_t p_pid,
        uint64_t p_max_file_size ) {
    m_dir_log = p_dir_log;
    m_pgm_name = p_pgm_name;
    m_pid = p_pid;
    m_max_file_size = p_max_file_size;
}


// -----------------------------------------------------------------------------
change_file_policy::
~change_file_policy( ) {

}

// -----------------------------------------------------------------------------
void
change_file_policy::
on_thread_end( ofstream & p_file ) {

    if ( p_file.is_open() ) {
        p_file.close( );
    }

    // const std::string l_log_file_name_ended ( m_current_file_name +
    //                                           std::string( ".END" ) );

    // std::rename( m_current_file_name.c_str( ),
    //              l_log_file_name_ended.c_str( ) );
    

    // std::string l_final_file_name = final_file_name( );
    // std::ofstream l_final_file( l_final_file_name.c_str( ) );

    // l_final_file << g_text_log_identifier << "\n";
    
    // for ( int32_t l_i = 0; l_i <= m_file_index; ++l_i ) {
    //     std::string l_file_name = file_name( l_i );

    //     std::ifstream l_file( l_file_name.c_str( ) );

    //     std::string l_line;

    //     // bypassing first line
    //     std::getline( l_file, l_line );

    //     while ( true ) {
    //         if ( l_file.eof( ) ) {
    //             break;
    //         }

    //         std::getline( l_file, l_line );

    //         l_final_file << l_line << "\n";
    //     }

    //     l_file.close( );
    //     std::remove( l_file_name.c_str( ) );
    // }
}


// -----------------------------------------------------------------------------
bool
change_file_policy::
change_now( uint64_t p_file_size ) const {
    static const double ninety_per_cent_max =
        0.9 * static_cast<double>( m_max_file_size );

    const double l_tmp = static_cast<double>( p_file_size );

    bool l_change = ( l_tmp > ninety_per_cent_max );
                
    return l_change;
}

// -----------------------------------------------------------------------------
const std::string & 
change_file_policy::
file_name( int32_t p_file_index ) {
    std::stringstream l_stream;

    l_stream << m_dir_log << "/" << m_pgm_name << "_" << m_pid << "_";

    if ( m_thread_id == boost::thread::id( ) ) {
        l_stream << "0";
    }
    else {

        int64_t l_thread_id = tenacitas::log::th2int( m_thread_id );

        l_stream << std::dec << l_thread_id;
    }

    l_stream << "_" << std::setfill( '0' ) << std::setw( 10 ) << p_file_index;

    l_stream << ".log";

    m_current_file_name = l_stream.str( );

    return m_current_file_name;
}

// -----------------------------------------------------------------------------
std::string
change_file_policy::
final_file_name( ) const {
    std::stringstream l_stream;

    l_stream << m_dir_log << "/" << m_pgm_name << "_" << m_pid << "_";

    if ( m_thread_id == boost::thread::id( ) ) {
        l_stream << "0";
    }
    else {

        int64_t l_thread_id = tenacitas::log::th2int( m_thread_id );

        l_stream << std::dec << l_thread_id;
    }

    l_stream << ".log.END";

    return l_stream.str( );
}


// #############################################################################
// config_logger

// -----------------------------------------------------------------------------
void tenacitas::text_log_file::config_logger( const std::string & p_dir_log,
                                              const std::string & p_pgm_name,
                                              std::int64_t p_pid,
                                              tenacitas::log::level p_level,
                                              uint64_t p_max_file_size ) {

    /** extracting pgm name out of full pgm name path */
    std::string l_full_pgm_name( p_pgm_name );

    std::string::size_type l_ind = l_full_pgm_name.rfind( '/' );
    if ( l_ind == std::string::npos ) {
        l_ind = l_full_pgm_name.rfind( '\\' );
    }

    std::string l_pgm_name;

    if ( l_ind == std::string::npos ) {
        l_pgm_name = l_full_pgm_name;
    }
    else {
        l_pgm_name = l_full_pgm_name.substr( l_ind + 1 );
    }

    /** configuring factories */
    change_file_policy::config( p_dir_log, l_pgm_name,
                                p_pid, p_max_file_size );

    logger::set_level_all( p_level );
}




// #include <tenacitas_text_log_file/logger.h> 

// using namespace tenacitas::text_log_file;



